package com.fdc_project.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fdc_project.entity.Productinformation;
import com.fdc_project.entity.Projectadress;
import com.fdc_project.entity.Staff;
import com.fdc_project.mapper.ProductinformationMapper;
import com.fdc_project.mapper.ProjectadressMapper;
import com.fdc_project.service.IProjectadressService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fdc_project.vo.ErrorCode;
import com.fdc_project.vo.Result;
import com.sun.applet2.preloader.event.ErrorEvent;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Sk
 * @since 2021-11-29
 */
@Service
public class ProjectadressServiceImpl extends ServiceImpl<ProjectadressMapper, Projectadress> implements IProjectadressService {


    @Autowired
    ProductinformationMapper productinformationMapper;

    @Autowired
    ProjectadressMapper projectadressMapper;



    @Override
    public Result addProAdress(String pro_name) {                                                   //通过项目名称建立地址信息
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("pro_name",pro_name);
        int pro_id = productinformationMapper.selectOne(queryWrapper).getProId();
        queryWrapper.clear();
        queryWrapper.eq("pro_id",pro_id);
        Projectadress projectadress1 = projectadressMapper.selectOne(queryWrapper);                 //查看地址信息表中是否含有该项目
        if(projectadress1 == null || projectadress1.getDeleted() == 1 ){                       //在表中未查到该项目id下的信息
            String pro_adress ="/Fdc_project/"+pro_name;                                       //项目总地址
            String Driectory[] = new String[]{pro_adress+"/jpg",pro_adress+"/后期文件/原始文件",
                    pro_adress+"/模型文件/原始模型",pro_adress+"/Fdc_project/模型文件/最终模型"
                    ,pro_adress+"/小样", pro_adress+"/渲染文件/原始渲染",
                    pro_adress+"/渲染文件/最终渲染","/"+pro_name+"/资料"};
            Projectadress projectadress = new Projectadress(
                    0,pro_id,pro_adress,
                    Driectory[0],Driectory[1],Driectory[2]
                    ,Driectory[3],Driectory[4],Driectory[5]
                    ,Driectory[6], Driectory[7],0);
            if (projectadressMapper.insert(projectadress) > 0) {
                return Result.success(projectadress);
            }else {
                return Result.fail(ErrorCode.PARAMS_ERROR.getCode(),"添加失败！");
            }
        }
        return Result.fail(ErrorCode.ACCOUNT_EXIST.getCode(),"该项目地址已经存在！");
    }

    @Override
    public Result delProAdress(Integer pro_id) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("pro_id",pro_id);
        Projectadress projectadress = projectadressMapper.selectOne(queryWrapper);          //根据项目id查询地址信息
        if(projectadress == null ||projectadress.getDeleted() == 1){
            return Result.fail(ErrorCode.USER_NULL.getCode(), "未查询到该项目地址！");
        }
        projectadress.setDeleted(1);
        if (projectadressMapper.updateById(projectadress) > 0) {
            return  Result.success(projectadress);
        }
        return Result.fail(ErrorCode.PARAMS_ERROR.getCode(), "删除失败！");
    }


    @Override
    public Result selectone(Integer id) {
        return null;
    }

    @Override
    public Result selectcall() {
        return null;
    }
}
